package com.example.test.advice.第一章_java开发中常用的方法和准则.建议14_使用序列化类的私有方法巧妙解决部分属性持久化问题;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = -5055019374583290925L;

    private String name;

    //加transient 序列化时忽略改属性
    private transient Salary salary;

    public Person(String name, Salary salary) {
        this.name = name;
        this.salary = salary;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();
        out.writeInt(salary.getBasePay());
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        salary = new Salary(in.readInt(),0);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Salary getSalary() {
        return salary;
    }

    public void setSalary(Salary salary) {
        this.salary = salary;
    }
}
